package com.fl.utils;

import com.alibaba.fastjson.JSON;
import com.fl.entity.UserPortrait;
import org.elasticsearch.action.index.IndexRequest;
import org.elasticsearch.action.search.SearchRequest;
import org.elasticsearch.common.xcontent.XContentType;
import org.elasticsearch.index.query.BoolQueryBuilder;
import org.elasticsearch.index.query.QueryBuilders;
import org.elasticsearch.search.builder.SearchSourceBuilder;
import org.elasticsearch.search.fetch.subphase.highlight.HighlightBuilder;

import java.text.SimpleDateFormat;
import java.util.*;
import java.util.stream.Collectors;

/**
 * Created with IntelliJ IDEA.
 *
 * @Author: fulin
 * @Date: 2021/09/04/11:31
 * @Description:
 */
public class QueryBuilder {

    private static String USER_PORTRAIT = "up_portrait";

    private static SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");

    private static List<String> storedField = new ArrayList<>();

    public static List<String> esConfig() {
        storedField.add("_source");
        storedField.add("*");
        return storedField;
    }

    //保存用户画像
    public IndexRequest saveUserPortrait(UserPortrait userPortrait){
        IndexRequest indexRequest = new IndexRequest(USER_PORTRAIT);
        Map<String, Object> map = new HashMap<>();
        map.put("userIdentify", userPortrait.getUserIdentify());
        map.put("productUsage", userPortrait.getProductUsage());
        map.put("other", userPortrait.getOther());
        map.put("createTime", format.format(userPortrait.getCreateTime()));
        indexRequest.id(userPortrait.getUserId()).source(map).create(false);
        return indexRequest;
    }

    //查询所有
    public SearchRequest searchUserPortrait() {

        SearchRequest searchRequest = new SearchRequest(USER_PORTRAIT);
        SearchSourceBuilder searchSourceBuilder = new SearchSourceBuilder();
        searchSourceBuilder.storedFields(esConfig()).storedField("userIdentify");
        searchRequest.source(searchSourceBuilder);
        return searchRequest;
    }

    //根据用户画像查询
    public SearchRequest searchUserPortrait(UserPortrait userPortrait) {

        SearchRequest searchRequest = new SearchRequest(USER_PORTRAIT);
        SearchSourceBuilder searchSourceBuilder = new SearchSourceBuilder();
        BoolQueryBuilder boolQueryBuilder = new BoolQueryBuilder();
        //用户身份是否为空
        if (userPortrait.getUserIdentify() != "") {
            boolQueryBuilder.filter(QueryBuilders.matchQuery("userIdentify", userPortrait.getUserIdentify()));
        }
        //产品途径是否为空
        if (userPortrait.getProductUsage() != null) {
            boolQueryBuilder.filter(QueryBuilders.termsQuery("productUsage", userPortrait.getProductUsage()));
        }
        //输入其他用途 是否为空
        if (userPortrait.getOther() != "") {
            boolQueryBuilder.filter(QueryBuilders.matchQuery("other", userPortrait.getOther() + "*"));
        }
        searchSourceBuilder.query(boolQueryBuilder).storedField("userIdentify");
        searchSourceBuilder.storedFields(esConfig());
        searchRequest.source(searchSourceBuilder);
        return searchRequest;
    }

    //查询时间区间内用户画像查询
    public SearchRequest searchUserPortrait(UserPortrait userPortrait, Date start, Date end) {

        SearchRequest searchRequest = new SearchRequest(USER_PORTRAIT);
        SearchSourceBuilder searchSourceBuilder = new SearchSourceBuilder();
        BoolQueryBuilder boolQueryBuilder = new BoolQueryBuilder();
        //用户身份是否为空
        if (userPortrait.getUserIdentify() != "") {
            boolQueryBuilder.filter(QueryBuilders.matchQuery("userIdentify", userPortrait.getUserIdentify()));
        }
        //产品途径是否为空
        if (userPortrait.getProductUsage() != null) {
            boolQueryBuilder.filter(QueryBuilders.termsQuery("productUsage", userPortrait.getProductUsage()));
        }
        //输入其他用途 是否为空
        if (userPortrait.getOther() != "") {
            boolQueryBuilder.filter(QueryBuilders.matchQuery("other", userPortrait.getOther() + "*"));
        }
        //时间区间内用户画像
        if (start != null && end != null) {
            boolQueryBuilder.filter(QueryBuilders.rangeQuery("createTime").gte(format.format(start)).lte(format.format(end)));
        }
        searchSourceBuilder.query(boolQueryBuilder).storedField("userIdentify");
        searchSourceBuilder.storedFields(esConfig());
        searchRequest.source(searchSourceBuilder);
        return searchRequest;
    }

    //查询时间区间内用户画像查询且分页
    public SearchRequest searchUserPortrait(UserPortrait userPortrait, Date start, Date end, int from, int showSize) {

        SearchRequest searchRequest = new SearchRequest(USER_PORTRAIT);
        SearchSourceBuilder searchSourceBuilder = new SearchSourceBuilder();
        BoolQueryBuilder boolQueryBuilder = new BoolQueryBuilder();
        //用户身份是否为空
        if (userPortrait.getUserIdentify() != "" && userPortrait.getUserIdentify() != null) {
            boolQueryBuilder.filter(QueryBuilders.matchQuery("userIdentify", userPortrait.getUserIdentify()));
        }
        //产品途径是否为空
        if (userPortrait.getUserIdentify() != "" && userPortrait.getProductUsage() != null) {
            boolQueryBuilder.filter(QueryBuilders.termsQuery("productUsage", userPortrait.getProductUsage()));
        }
        //输入其他用途 是否为空
        if (userPortrait.getOther() != "" && userPortrait.getOther() != null) {
            boolQueryBuilder.filter(QueryBuilders.matchQuery("other", userPortrait.getOther() + "*"));
        }
        //若大小为0必定未分页
        if (showSize != 0) {
            searchSourceBuilder.from(from);
            searchSourceBuilder.size(showSize);
        }
        //时间区间内用户画像
        if (start != null && end != null) {
            boolQueryBuilder.filter(QueryBuilders.rangeQuery("createTime").gte(format.format(start)).lte(format.format(end)));
        }
        searchSourceBuilder.query(boolQueryBuilder).storedField("userIdentify");
        searchSourceBuilder.storedFields(esConfig());
        searchRequest.source(searchSourceBuilder);
        return searchRequest;
    }

    //查询所有
    public SearchRequest search() {

        SearchRequest searchRequest = new SearchRequest(USER_PORTRAIT);
        SearchSourceBuilder searchSourceBuilder = new SearchSourceBuilder();
        searchSourceBuilder.storedFields(esConfig()).storedField("userIdentify");
        BoolQueryBuilder boolQueryBuilder = new BoolQueryBuilder();
        boolQueryBuilder.filter(QueryBuilders.matchQuery("other","现代"));

        HighlightBuilder highlightBuilder = new HighlightBuilder();
        HighlightBuilder.Field highlightTitle = new HighlightBuilder.Field("other");
        highlightTitle.preTags("<font color='red'>");
        highlightTitle.postTags("</font>");
        highlightBuilder.field(highlightTitle);
        searchSourceBuilder.highlighter(highlightBuilder);
        searchSourceBuilder.query(boolQueryBuilder);
        searchRequest.source(searchSourceBuilder);
        return searchRequest;
    }



}
